/**
 * Created by hooshim on 2014-12-04.
 */

var G_grid01 = "";
var G_page = 1;

$(document).ready(function() {
    fnGetSubMenus();
    fnSetCurMenu(5);
    fnGetLeftMenus('4');
    $("#_curMenu p").html("거래처")

    var bodyWidth = $('body').innerWidth();
    bodyWidth = parseInt(bodyWidth,10) - 170;
    $('#_bodyRight').css('width', bodyWidth + 'px');
    var gSubMenus = fnGetSubMenus();
    var gScreenHeight = parseInt($(window).height(),10) - 120;
    $("#_bodyLeft").css("height",gScreenHeight);
    $("#_bodyRight").css("height",gScreenHeight);
    gScreenHeight = parseInt(gScreenHeight,10) - 100;

    fnSetGrid01(gScreenHeight);
    fnGetList01(1);
});

var fnSetGrid01 = function(h) {

    var grdCd = "grid01";

    var widths = [120, 70, 100, 100, 70, 100, 100, 170, 80, 50];
    var titles = ['회사명', '대표자', '연락처', 'FAX', '담당자', '휴대전화', '담당직통', '영문사명', '사업자번호', '거래여부'];
    var styles = ['text-align:left;', 'text-align:center;', 'text-align:center;', 'text-align:center;','text-align:center;', 'text-align:center;', 'text-align:center;', 'text-align:left;', 'text-align:center;', 'text-align:center;'];

    var gInfo = new Object();
    gInfo["grdCd"] = "grid01";
    gInfo["title"] = "거래처";
    gInfo["widths"] = widths;
    gInfo["titles"] = titles;
    gInfo["styles"] = styles;
    gInfo["function"] = "fnShowCompany";
    gInfo["fullWidth"] = "445";
    gInfo["fullHeight"] = h;
    gInfo["fix"] = "1";

    G_grid01 = JSON.stringify(gInfo);

    fnMkGridInit(G_grid01);
}

var fnGetList = function() {

    if(event.keyCode != "13")
    {
        return; // 엔터일때만 수행
    }
    fnGetList01(1);
}

var fnGetList01 = function(page) {
    fnMkGridLoad(G_grid01);
    if (page) G_page = page;
    else page = G_page;
    var param  = "findCompany="+$("#findComapny").val();
    var valUrl = "./action/company/list.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){

            fnMkGrid(G_grid01,data);
            /*
             $("#table_body").html(list);
             $("#total_rows").html(data.TOTAL_ROW_COUNT);
             var paging = fnPaging('fnGetList', data.TOTAL_ROW_COUNT, page, 10);
             var pages = paging['prev'] + paging['pager'] + paging['next'];
             // alert(pages);
             $("#pager").html(pages);
             */
        },
        error: function(data){
            alert('Error!!');
            alert(data.responseText);
        }
    });
}

var fnShowCompany = function(compCd) {

    var param  = "compCd="+compCd;
    var valUrl = "./action/company/view.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){
            fnReset();

            var dataDetail = data.TBLCOMPANY[0];
            $("#compCd").val(dataDetail.COMPCD);
            $("#companyKR").val(dataDetail.COMPANYKR);
            $("#owner").val(dataDetail.OWNER);
            $("#phone").val(dataDetail.PHONE);
            $("#fax").val(dataDetail.FAX);
            $("#email").val(dataDetail.EMAIL);
            $("#addrKR").val(dataDetail.ADDRKR);
            $("#compNo").val(dataDetail.COMPNO);
            $("#compType").val(dataDetail.COMPTYPE);
            $("#compPart").val(dataDetail.COMPPART);
            $("#flag").val(dataDetail.FLAG);

            $("#companyEN").val(dataDetail.COMPANYEN);
            $("#addrEN").val(dataDetail.ADDREN);

            $("#major").val(dataDetail.MAJOR);
            $("#cellPhone").val(dataDetail.CELLPHONE);
            $("#majorPhone").val(dataDetail.MAJORPHONE);
            $("#memo").val(dataDetail.MEMO);

            $("#oilRate").val(data.OIL);
            $("#emsRate").val(data.EMS);
            $("#dhlRate").val(data.DHL);
            $("#upsRate").val(data.UPS);
            $("#tntRate").val(data.TNT);
        },
        error: function(request,status,error){
            alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
        }
    });
}

var fnSave = function() {
    if (!$("#companyKR").val()) {
        alert("회사명을 바르게 입력해 주세요.");
        $("#companyKR").focus();
        return;
    }
    var param = "compCd=" + $("#compCd").val();
    param += "&companyKR=" + $("#companyKR").val();
    param += "&owner=" + $("#owner").val();
    param += "&phone=" + $("#phone").val();
    param += "&fax=" + $("#fax").val();
    param += "&email=" + $("#email").val();
    param += "&addrKR=" + $("#addrKR").val();
    param += "&compNo=" + $("#compNo").val();
    param += "&compType=" + $("#compType").val();
    param += "&compPart=" + $("#compPart").val();
    param += "&flag=" + $("#flag").val();
    param += "&companyEN=" + $("#companyEN").val();
    param += "&addrEN=" + $("#addrEN").val();
    param += "&major=" + $("#major").val();
    param += "&cellPhone=" + $("#cellPhone").val();
    param += "&majorPhone=" + $("#majorPhone").val();
    param += "&memo=" + $("#memo").val();
    param += "&oilRate=" + $("#oilRate").val();
    param += "&emsRate=" + $("#emsRate").val();
    param += "&upsRate=" + $("#upsRate").val();
    param += "&dhlRate=" + $("#dhlRate").val();
    param += "&tntRate=" + $("#tntRate").val();

    var valUrl = "./action/company/save.php";

    $.ajax({
        url: valUrl,
        type: 'POST',
        dataType: 'json',
        data: param,
        success: function(data){
            if (data.rsStatus == "Success") {
                alert(data.rsStr);
                fnReset();
                fnGetList01();
            }
            else {
                alert("저장 실패");
            }
        },
        error: function(request,status,error){
            alert("code:"+request.status+"\n"+"message:"+request.responseText+"\n"+"error:"+error);
        }
    });
}

var fnReset = function() {

    $("#compCd").val('');
    $("#companyKR").val('');
    $("#owner").val('');
    $("#phone").val('');
    $("#fax").val('');
    $("#email").val('');
    $("#addrKR").val('');
    $("#compNo").val('');
    $("#compType").val('');
    $("#compPart").val('');
    $("#flag").val('Y');


    $("#companyEN").val('');
    $("#addrEN").val('');

    $("#major").val('');
    $("#cellPhone").val('');
    $("#majorPhone").val('');
    $("#memo").val('');

    $("#oilRate").val('0');
    $("#emsRate").val('0');
    $("#dhlRate").val('0');
    $("#upsRate").val('0');
    $("#tntRate").val('0');
}
